package com.oracle.mall.controller;

import com.oracle.mall.po.CartDetail;
import com.oracle.mall.po.Member;
import com.oracle.mall.po.Nav;
import com.oracle.mall.service.AccountService;
import com.oracle.mall.service.CartService;
import com.oracle.mall.service.NavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AccountController {

    @Autowired
    private CartService cartService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private NavService navService;

    @RequestMapping("/login_in")
    public String login(Model model, String name, String pass, HttpSession session){

        List<Nav> nav = navService.getNav();
        model.addAttribute("nav",nav);

        Member member = accountService.login(name,pass);
        if (member != null){
            session.setAttribute("member",member);
        }
        return "redirect:index";
    }

    @RequestMapping("/logout")
    public String logout(Model model,HttpSession session){
        List<Nav> nav = navService.getNav();
        model.addAttribute("nav",nav);
        session.invalidate();
        return "redirect:index";
    }


    @RequestMapping("/login")
    public String index(Model model, HttpSession session){//参数应该还有page和字符串

        if(session.getAttribute("member")!=null){
            Member member = (Member) session.getAttribute("member");
            Integer mid = member.getMid();
            CartDetail cartDetail = cartService.queryTotal(mid);
            model.addAttribute("cartDetail",cartDetail);
        }

        List<Nav> nav = navService.getNav();
        //将查询到的导航数据存入request作用域
        model.addAttribute("nav",nav);
        return "login";


    }


}
